// import { createRouter, createWebHistory } from 'vue-router'

// const router = createRouter({
//   history: createWebHistory(import.meta.env.BASE_URL),
//   routes: [
//     {
//       path: '/',
//       name: 'home',
//       component: () => import('../components/Home/home.vue')
//     },
//     {
//       path: '/about',
//       name: 'about',
//       // route level code-splitting
//       // this generates a separate chunk (About.[hash].js) for this route
//       // which is lazy-loaded when the route is visited.
//       component: () => import('../views/AboutView.vue')
//     },
//     {
//       path: '/user',
//       name: 'Index',
//       component: () => import('../views/Index.vue')
//     },
//     {
//       path: "/forgotPassword",
//       name: "forgotPassword",
//       component: () => import("../components/User/forgotPassword.vue")
//     }, {
//       path: "/resetpassword/:id",
//       name: "resetpassword",
//       component: () => import("../components/User/resetPassword.vue")
//     }, {
//       path: '/:catchAll(.*)',
//       name: '404',
//       component: () => import('../views/404.vue')
//     }
//   ]
// })

// export default router

import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/home',
    //   name: 'home',
    //   component: () => import('../components/Home/home.vue'),
    //   meta: { requiresAuth: true } // 需要身份验证
    // },
    // {
    //   path: '/about',
    //   name: 'about',
    //   component: () => import('../views/AboutView.vue'),
    //   meta: { requiresAuth: true } // 需要身份验证
    // },
    {
      path: '/user',
      name: 'Index',
      component: () => import('../views/Index.vue')
    },
    {
      path: "/forgotPassword",
      name: "forgotPassword",
      component: () => import("../components/User/forgotPassword.vue")
    },
    {
      path: '/:catchAll(.*)',
      name: '404',
      component: () => import('../views/404.vue')
    },
    {
      path: '/',
      name: 'Manager',
      component: () => import('../views/Manager.vue'),
      redirect: '/home',  // 重定向到主页
      children: [
        { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/Home.vue') },
        { path: 'about', name: 'About', meta: { name: '关于' }, component: () => import('../views/About.vue') },
      ],
      meta: { requiresAuth: true }
    }
  ]
});

// 假设你有一个方法来检查用户是否已登录
function isAuthenticated() {
  // const token = localStorage.getItem('msToken')
  // console.log(token !== null && token !== undefined)
  return !!localStorage.getItem('msToken'); // 检查 localStorage 中是否有 mstoken
  // return token !== null && token !== undefined;
}


// 添加路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !isAuthenticated()) {
    next({ name: 'Index' }); // 假设你有一个登录路由
  } else {
    next();
  }
});

export default router;
